package com.beimi.disruptor;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import com.beimi.util.disruptor.UserDataEventFactory;
import com.beimi.util.disruptor.UserEventHandler;
import com.beimi.util.event.UserDataEvent;
import com.lmax.disruptor.SleepingWaitStrategy;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;

/**
 * 生成消费模式
 * 
 */
@Component
public class DisruptorConfigure {
	@SuppressWarnings({ "unchecked", "deprecation" })
	@Bean(name = "disruptor")
	public Disruptor<UserDataEvent> disruptor() {
		Executor executor = Executors.newCachedThreadPool();
		UserDataEventFactory factory = new UserDataEventFactory();
		Disruptor<UserDataEvent> disruptor = new Disruptor<UserDataEvent>(factory, 1024, executor, ProducerType.SINGLE,
				new SleepingWaitStrategy());
		disruptor.setDefaultExceptionHandler(new BeiMiExceptionHandler());
		disruptor.handleEventsWith(new UserEventHandler());
		disruptor.start();
		return disruptor;
	}
}
